import { map } from "@/store";

import zhibeibg from "@/assets/img/zhibeibg.png";
import zhibei from "@/assets/img/zhibei.png";
import { locationObj } from "@/components/controls/location/Location";
import { zoomObj } from "@/components/controls/zoom/Zoom";
import { floorObj } from "@/components/controls/floor/FloorControl";
import { layersObj } from "@/components/controls/layers/Layers";

let compass;
let zoom;
let position;
let floor;

const addControls = () => {
  // addCompassControl();
  addZoomControl();
  addScrollFloorControl();
  addPositionControl();
};

//添加指北针
const addCompassControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  // 指北针
  // @ts-ignore
  compass = new fengmap.FMCompass({
    backgroundImageURL: zhibeibg,
    pointerImageURL: zhibei,
    offset: {
      x: 15,
      y: 15,
    },
    // @ts-ignore
    position: fengmap.FMControlPosition.LEFT_TOP,
  });
  // @ts-ignore
  compass.addTo(map.fmap);
  compass.on("click", function () {
    map.fmap.setRotation({
      rotation: 0,
      animate: true,
      duration: 0.3,
      finish: function () {
        console.log("setRotation");
      },
    });
  });
};
//删除指北针
const removeCompassControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  try {
    compass.remove();
  } catch (e) {}
};
//添加缩放控制器
const addZoomControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  // @ts-ignore
  // zoom = new fengmap.FMZoomControl({
  //   // @ts-ignore
  //   position: fengmap.FMControlPosition.LEFT_BOTTOM,
  //   offset: {
  //     x: 15,
  //     y: -110,
  //   },
  // });
  // zoom.addTo(map.fmap);
  zoomObj.isShow = true;
};
//删除缩放控制器
const removeZoomControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  try {
    // zoom.remove();
    zoomObj.isShow = false;
  } catch (e) {}
};
//添加楼层控制器
const addScrollFloorControl = () => {
  if (!map.status.map.load) {
    return false;
  }

  // var scrollFloorCtlOpt = {
  //   // @ts-ignore
  //   position: fengmap.FMControlPosition.RIGHT_BOTTOM,
  //   floorButtonCount: 5,
  //   offset: {
  //     x: -15,
  //     y: -110,
  //   },
  // };
  // // @ts-ignore
  // floor = new fengmap.FMToolbar(scrollFloorCtlOpt);
  // floor.addTo(map.fmap);
  floorObj.isShow = true;
  layersObj.isShow = true;
};
//删除楼层控制器
const removeScrollFloorControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  try {
    // floor.remove();
    floorObj.isShow = false;
    layersObj.isShow = false;
  } catch (e) {}
};
//添加定位控制器
const addPositionControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  locationObj.isShow = true;
};
//删除定位控制器
const removePositionControl = () => {
  if (!map.status.map.load) {
    return false;
  }
  locationObj.isShow = false;
};
const removeControls = () => {
  // removeCompassControl();
  removeZoomControl();
  removeScrollFloorControl();
  removePositionControl();
};

export default {
  addControls,
  addCompassControl,
  addZoomControl,
  addScrollFloorControl,
  addPositionControl,
  removeControls,
  removeCompassControl,
  removeZoomControl,
  removeScrollFloorControl,
  removePositionControl,
};
